/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { watchWorker } from '@ohos/hvigor';
import { FileSet } from '@ohos/hvigor';
import { Level } from 'log4js';
import path from 'path';

import { OhosLogger } from '../../utils/log/ohos-logger.js';

import { LegacyCompileNode } from './legacy-compile-node.js';

export class LegacyPreviewerArkCompile extends LegacyCompileNode {
  logger: OhosLogger = OhosLogger.getLogger(LegacyPreviewerArkCompile.name);

  declareInputFiles(): FileSet {
    return this.moduleModel.isHarModule() ? super.declareInputFiles().deleteEntry(this.aceBuildJsonDir) : super.declareInputFiles();
  }

  protected async doTaskAction(): Promise<void> {
    const compileConfig = await this.getPreviewCompileConfig(this.logger);
    compileConfig.aceModuleJsonPath = undefined;
    const workerId = watchWorker.createWorker(
      this,
      path.resolve(__dirname, '../worker/preview-build.js'),
      true,
      (compileEvents: object[]) => this.handleCompileEvents(compileEvents, this.getSubDurationEvent(workerId), true),
      {
        workerData: {
          projectConfig: compileConfig,
          logLevel: (this.logger.getLevel() as Level).levelStr,
        },
      },
    );
  }

  initTaskDepends(): void {
    this.dependsOnHook('buildPreviewerResource');
  }
}
